package com.serena.da.rest.data_providers.xml;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;

/**
 * Created by Spitkovskyi Igor on 8/19/2014.
 */
public class XmlDataProvider {

    protected static NodeList getDatasetsFromXml(String FilenamePath, String SchemafilePath, String DatasetNodeName, int NumberOfRequiredParameters) throws Exception
    {
        NodeList ComponentNodesList = getXmlNodesByName(FilenamePath, SchemafilePath, DatasetNodeName);

        for(int i=0; i<ComponentNodesList.getLength(); i++){
            NodeList ComponentParametersList = ComponentNodesList.item(i).getChildNodes();
            if(ComponentParametersList.getLength() < NumberOfRequiredParameters){
                continue;
            }
            //Remove "\n\r"-like nodes from the nodes list. Leave only nodes which are ELEMENTS
            removeNotElementNodes(ComponentParametersList);
        }

        return ComponentNodesList;
    }

    //This method just extracts elements by name specified from the XML file passed as parameter.
    protected static NodeList getXmlNodesByName(String filenamePath, String schemafilePath, String ByElementName) throws Exception{
        boolean dtdValidate = false;
        boolean xsdValidate = false;
        //8String schemaSource = null;

        if(filenamePath.isEmpty()){
            throw new Exception ("Path to the data-file should have been provided.");
        }

        if(ByElementName.isEmpty()){
            throw new Exception ("Name of node to be searched for should have been provided");
        }

        String filename = filenamePath;
        if (schemafilePath.toLowerCase().equals("dtd"))  {
            dtdValidate = true;
        }
        else if (schemafilePath.toLowerCase().equals("xsd")) {
            xsdValidate = true;
        }
        /*else if (schemafilePath.toLowerCase().equals("xsdss")) {
            if (i == args.length - 1) {
                usage();
            }
            xsdValidate = true;
            schemaSource = args[++i];
        }*/

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(dtdValidate || xsdValidate);

        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File(filename));
        NodeList elements = doc.getElementsByTagName(ByElementName);

        return elements;
    }

    protected static void removeNotElementNodes(NodeList nodes){
        /* This method will just go through all the nodes passed in the NodeList and determine if each node is of ELEMENT_NODE type
            If some element is NOT of the ELEMENT_NODE type, it will be removed from the parent node's tree
            This is need to avoid usage of "\n\r" or blank-space text nodes.
            EXAMPLE:  http://www.java2s.com/Code/Java/XML/Getchildfromanelementbyname.htm
        */
        for(int i=0; i<nodes.getLength(); ){
            Node node = nodes.item(i);
            if(node.getNodeType() != Node.ELEMENT_NODE){
                node.getParentNode().removeChild(node);
                continue;
            }
            i++;
        }
    }
}


